package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class StudentForm extends ActionForm {
	
	// ============== Debug Flag ================= //
	private static final boolean DEBUG = true;
	
	// =============== Form Variables ============= //
	private String fname = null;
	private String lname = null;
	private String mname = null;
	private String citizen = null;
	private String residence = null;
	private String address = null;
	private String city = null;
	private String state = null;
	private String zip_code = null;
	private String country_code = null;
	private String area_code = null;
	private String telephone = null;
	private String residency = null;
	private String specialization = null;
	
	// =============== Getter and Setters ================ //
	public String getFname() {
		return fname;
	}
	public void setFname(String fname) {
		this.fname = fname;
	}
	public String getLname() {
		return lname;
	}
	public void setLname(String lname) {
		this.lname = lname;
	}
	public String getMname() {
		return mname;
	}
	public void setMname(String mname) {
		this.mname = mname;
	}
	
	public String getCitizen() {
		return citizen;
	}
	public void setCitizen(String citizen) {
		this.citizen = citizen;
	}
	
	public String getResidence() {
		return residence;
	}
	public void setResidence(String residence) {
		this.residence = residence;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getZip_code() {
		return zip_code;
	}
	public void setZip_code(String zip_code) {
		this.zip_code = zip_code;
	}
	public String getCountry_code() {
		return country_code;
	}
	public void setCountry_code(String country_code) {
		this.country_code = country_code;
	}
	public String getArea_code() {
		return area_code;
	}
	public void setArea_code(String area_code) {
		this.area_code = area_code;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getResidency() {
		return residency;
	}
	public void setResidency(String residency) {
		this.residency = residency;
	}
	
	public String getSpecialization() {
		return specialization;
	}
	public void setSpecialization(String specialization) {
		this.specialization = specialization;
	}
	
	/**
	 * Reset all properties to their default values.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		this.setFname(null);
		this.setMname(null);
		this.setLname(null);
		this.setCitizen(null);
		this.setResidence(null);
		this.setAddress(null);
		this.setCity(null);
		this.setState(null);
		this.setZip_code(null);
		this.setCountry_code(null);
		this.setArea_code(null);
		this.setTelephone(null);
		this.setResidency(null);
		this.setSpecialization(null);
	}
	
	/**
	 * Validate the input data. If validation fails, then report the errors in
	 * students.jsp, and specifically where <html:errors/> is located.
	 * 
	 * @param mapping
	 *            The mapping used to select this instance
	 * @param request
	 *            The servlet request we are processing
	 */
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {

		ActionErrors errors = new ActionErrors();
		String input = mapping.getInput();
		String path = mapping.getPath();
		
		if (DEBUG) {
			System.err.println("StudentForm: "  + input);
		}
		// Student name page 
		if (input.equals("/students/name.jsp")) {
			
			// Check first name
			if ((fname == null) || (fname.length() < 1) ) {
				errors.add("firstMsgTag1", new ActionMessage("errors.required",
						"First Name"));
			}	
			else if (isAllString(fname) == false) {
				errors.add("firstMsgTag2", new ActionMessage("error.name.number", 
						"First Name"));
			}
			
			// Check last name
			if ((lname == null) || (lname.length() < 1)) {
				errors.add("lastMsgTag1", new ActionMessage("errors.required",
						"Last Name"));
			}	
			else if (isAllString(lname) == false) {
				errors.add("lastMsgTag2", new ActionMessage("error.name.number", 
						"Last Name"));
			}
			
			// Check middle name
			if ( (mname != null) && (mname.length() > 0) && !isAllString(mname)) {
				errors.add("middleMsgTag1", new ActionMessage("error.name.number", 
					"Middle Name"));
			}	
		}
		else if (path.equals("/residency")) {
			
			// Check address
			if ( (address == null) || (address.length() < 1) ) {
				errors.add("addressMsgTag1", new ActionMessage("errors.required",
						"Address"));
			}
			
			// Check city
			if ( (city == null) || (city.length() < 1) ) {
				errors.add("addressMsgTag1", new ActionMessage("errors.required",
					"City"));
			}
			
			// Check state
			if ( ((country_code == null) || (country_code.length() < 1)) &&  
					((state == null) || (state.length() < 1))) {
				errors.add("stateMsgTag1", new ActionMessage("errors.required",
				"State"));
			}
			
			// Check zip code
			if ( (zip_code == null) || (zip_code.length() < 1) ) {
				errors.add("addressMsgTag1", new ActionMessage("errors.required",
					"Zip Code"));
			}
			else if (!isNum(zip_code)) {
				errors.add("addressMsgTag1", new ActionMessage("errors.notanumber",
				"Zip Code"));
			}
			
			// Check area code
			if ( (area_code == null) || (area_code.length() < 1) ) {
				errors.add("addressMsgTag1", new ActionMessage("errors.required",
					"Area Code"));
			}
			else if (!isNum(area_code)) {
				errors.add("addressMsgTag1", new ActionMessage("errors.notanumber",
				"Area Code"));
			}
			
			// Check telephone
			if ( (telephone == null) || (telephone.length() < 1) ) {
				errors.add("addressMsgTag1", new ActionMessage("errors.required",
					"Telephone"));
			}
			else {
				removeDashesAndSpaces(telephone);
				
				if (!isNum(telephone)) {
					errors.add("addressMsgTag1", new ActionMessage("errors.notanumber",
						"Telephone"));
				}
			}
		}
		
		return errors;
	}
	
	//============== Helper Functions ==============//
	private boolean isAllString(String string) {
		
		for(int index = 0; index < string.length(); index++) {
			if (Character.isDigit(string.charAt(index))) {
				return false;
			}	
		}
		
		return true;
	}
	
	private boolean isNum(String string) {
		
		for(int index = 0; index < string.length(); index++) {
			if (!Character.isDigit(string.charAt(index))) {
				return false;
			}	
		}
		
		return true;
	}
	
	private void removeDashesAndSpaces(String string) {
		
		String temp = "";
		char temp_c;
		
		for (int index = 0; index < string.length(); index++) {
			
			temp_c = string.charAt(index);
			
			if ( temp_c != ' ' && temp_c != '-') {
				temp += temp_c;
			}
		}
		
		if (temp.length() < string.length()) {
			telephone = temp;
			
			if (DEBUG) {
				System.err.println("StudentForm: new telephone(" + telephone + ")");
			}
		}
	}
}
